﻿using System;
using System.Linq;
using Telerik.Web.UI;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;

namespace Thuoc24h.AdminCP.Pages
{
    public partial class SlideEdit : BasePage.BasePages
    {
        private int _id;
        private Slide _obj = new Slide();

        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Convert.ToInt32(Request.QueryString["ID"]);
            if (!Page.IsPostBack)
            {


                if (_id > 0)
                {
                    btnSave.Text = ActionNames.Update;
                    BindData();
                }
                else
                {
                    btnSave.Text = ActionNames.AddNew;
                }
            }

        }

        private void GetData()
        {
            _obj.LinkUrl = txtLink.Text;
            _obj.ImageUrl = txtFileName.Text;
            _obj.Title = txtName.Text;
            _obj.SortOrder = radNumberOrd.Text.ToInt32Return0();
        }
        private void Reset()
        {
            txtLink.Text = string.Empty;
            txtFileName.Text = string.Empty;
            txtName.Text = string.Empty;
            radNumberOrd.Text = "1";
        }
        private void SaveData()
        {
            if (_id > 0)
            {
                _obj = context.Slides.SingleOrDefault(item => item.Id == _id);
                if (_obj != null)
                {
                    GetData();
                }

            }
            else
            {
                GetData();
                context.Slides.AddObject(_obj);
            }

            if (context.SaveChanges() > 0)
            {

                Reset();
                Response.Redirect(UrlManager.Admin.SlideView);
            }
        }
        private void BindData()
        {
            _obj = ReviewRepository.SlideById(_id);
            txtLink.Text = _obj.LinkUrl;
            txtFileName.Text = _obj.ImageUrl;
            txtName.Text = _obj.Title;
            radNumberOrd.Text = _obj.SortOrder.ToString();
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveData();

        }

        protected void OnClick(object sender, EventArgs e)
        {
            Reset();
        }
        
    }
}